app.controller('UpKeepingCtrl', function($rootScope, $ionicConfig,$animateCss, $ionicPopup, $scope, $state, $stateParams, $timeout, $interval, BthSevice, radialIndicatorInstance,$http,$myconfig) {
     //获取当前登录用户信息
    $scope.userMes = JSON.parse(localStorage.getItem('user'));

    //定义保养时间记时默认时间
    $scope.HYBsec = 0;

    //获取当前系统时间
    var ThisTime = new Date();


    //定义保养模式默认名
    var mode;
    //定义设备名
    var deviceName = '焕颜宝';

    // 定义默认保养状态
    var maintain_sta = 0;



    var type = $stateParams.type;
    var valuesArr = [];
    var typeTime = 5;


    //开始保养
    function doIt(){
    switch (type){
        case 'jubu': // 抗皱美白
        mode = '局部光压';
            $rootScope.mode = '局部光压';
        valuesArr = kangzhou;
            $scope.title = '局部光压';
            $scope.typeSrc = 'img/icons/kangzhou.svg';
            typeTime = $rootScope.Yzb_time;
            break;
        case 'runfu': //
            mode = '润肤嫩肤';
            $rootScope.mode = '润肤嫩肤';
            valuesArr = quban;
            $scope.title = '润肤嫩肤';
            $scope.typeSrc = 'img/icons/runfu.svg';

            typeTime = $rootScope.Yzb_time;
            break;
        case 'quban':
            mode = '祛斑祛痘';
            $rootScope.mode = '祛斑祛痘';
            valuesArr = runfu;
            $scope.title = '祛斑祛痘';
            $scope.typeSrc = 'img/icons/qunban.svg';
            typeTime = $rootScope.Yzb_time;
            break;
        default: // 面膜导入
            mode = '面膜导入';
            $rootScope.mode = '面膜导入';
            valuesArr = daomo;
            $scope.title = '面膜导入';
            $scope.typeSrc = 'img/icons/daomo.svg';
            typeTime = $rootScope.Yzb_time;
    }
};
    
    var kangzhou,quban,runfu,daomo;

    $scope.deviceLED = function(){
        $http.get($myconfig.api.base_host + '/device/list_device_b')
        .then(function(res){
            for(var i = 0;i<res.data.data.length;i++){
                if(res.data.data[i].name == '焕颜宝'){
                    for(var j = 0;j<res.data.data[i].mode.length;j++){
                        // alert(res.data.data[i].mode[j].colormode);
                        var mode;
                        if(res.data.data[i].mode[j].colormode == 0){
                            mode = ['0x7e', '0x05', '0x03', '0x9d', '0x03', '0xff', '0xff', '0x00', '0xef'];
                        }
                        if(j == 0){
                            kangzhou = [
                                // [0x7e, 0x04, 0x04, '0x01', 0xff, 0xff, 0xff, '0x00', '0xef'],//开关
                                ['0x7e', '0x04', '0x01', '0x' + parseInt(res.data.data[i].mode[j].redlight).toString(16), '0xff', '0xff', '0xff', '0x00', '0xef'],//亮度
                                ['0x7e', '0x04', '0x02', '0x' + parseInt(res.data.data[i].mode[j].speed).toString(16), '0xff', '0xff', '0xff', '0x00', '0xef'], // 设置速度
                                // ['0x7e', '0x07', '0x05', '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0xe6', '0x' + parseInt(res.data.data[i].mode[j].bluescale,16), 0x4d, '0x00', '0xef'] // 设置颜色强度
                                ['0x7e', '0x07', '0x05', '0x03', '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0x' + parseInt(res.data.data[i].mode[j].bluescale,16), '0x00', '0xef']
                            ];
                            if(res.data.data[i].mode[j].colormode == 0){
                                // alert(kangzhou);
                                kangzhou.push(mode);
                            }
                        }else if(j == 1){
                            quban = [
                                // ['0x7e', '0x04', '0x04', '0x01', '0xff', '0xff', '0xff', '0x00', '0xef'],//开关
                                ['0x7e', '0x04', '0x01', '0x' + parseInt(res.data.data[i].mode[j].redlight).toString(16), '0xff', '0xff', '0xff', '0x00', '0xef'],//亮度
                                ['0x7e', '0x04', '0x02', '0x' + parseInt(res.data.data[i].mode[j].speed).toString(16), '0xff', '0xff', '0xff', '0x00', '0xef'], // 设置速度
                                // ['0x7e', '0x07', '0x05', '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0xe6', '0x' + parseInt(res.data.data[i].mode[j].bluescale,16), 0x4d, '0x00', '0xef'] // 设置颜色强度
                                // ['0x7e', '0x05', '0x03', '0x' + parseInt(res.data.data[i].mode[j].colormode,16), '0x03', '0xff', '0xff', '0x00', '0xef']  // 设置模式
                                ['0x7e', '0x07', '0x05', '0x03', '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0x' + parseInt(res.data.data[i].mode[j].bluescale,16), '0x00', '0xef']
                            ];
                            if(res.data.data[i].mode[j].colormode == 0){
                                // alert(quban);
                                quban.push(mode);
                            }
                        }else if(j == 2){
                            runfu = [
                                // ['0x7e', '0x04', '0x04', '0x01', '0xff', '0xff', '0xff', '0x00', '0xef'],//开关
                                ['0x7e', '0x04', '0x01', '0x' + parseInt(res.data.data[i].mode[j].redlight).toString(16), '0xff', '0xff', '0xff', '0x00', '0xef'],//亮度
                                ['0x7e', '0x04', '0x02', '0x' + parseInt(res.data.data[i].mode[j].speed).toString(16), '0xff', '0xff', '0xff', '0x00', '0xef'], // 设置速度
                                // ['0x7e', '0x07', '0x05', '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0xe6', '0x' + parseInt(res.data.data[i].mode[j].bluescale,16), 0x4d, '0x00', '0xef'], // 设置颜色强度
                                // ['0x7e', '0x05', '0x03', '0x' + parseInt(res.data.data[i].mode[j].colormode,16), '0x03', '0xff', '0xff', '0x00', '0xef']  // 设置模式
                                ['0x7e', '0x07', '0x05', '0x03', '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0x' + parseInt(res.data.data[i].mode[j].bluescale,16), '0x00', '0xef']
                            ];
                            if(res.data.data[i].mode[j].colormode == 0){
                                // alert(runfu);
                                runfu.push(mode);
                            }
                        }else if(j == 3){
                            daomo = [
                                // ['0x7e', '0x04', '0x04', '0x01', '0xff', '0xff', '0xff', '0x00', '0xef'],//开关
                                ['0x7e', '0x04', '0x01', '0x' + parseInt(res.data.data[i].mode[j].redlight).toString(16), '0xff', '0xff', '0xff', '0x00', '0xef'],//亮度
                                ['0x7e', '0x04', '0x02', '0x' + parseInt(res.data.data[i].mode[j].speed).toString(16), '0xff', '0xff', '0xff', '0x00', '0xef'], // 设置速度
                                // ['0x7e', '0x07', '0x05', '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0xe6', '0x' + parseInt(res.data.data[i].mode[j].bluescale,16), 0x4d, '0x00', '0xef'], // 设置颜色强度
                                // ['0x7e', '0x05', '0x03', '0x' + parseInt(res.data.data[i].mode[j].colormode,16), '0x03', '0xff', '0xff', '0x00', '0xef']  // 设置模式
                                ['0x7e', '0x07', '0x05', '0x03', '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0x' + parseInt(res.data.data[i].mode[j].redscale).toString(16), '0x' + parseInt(res.data.data[i].mode[j].bluescale,16), '0x00', '0xef']
                            ];
                            if(res.data.data[i].mode[j].colormode == 0){
                                // alert(daomo);
                                daomo.push(mode);
                            }
                        }
                    }
                }
            }
            doIt();
        });
    };
    $scope.deviceLED();


    /*var animator = $animateCss('.zhuan', {
        from: { background:'red' },
        to: { background:'blue' }
    });*/
    // animator.start();


    // var data = [0x7e, 0x05, 0x03, 0x80, 0x03, 0xff, 0xff, 0x00, 0xef]; // 设置RGB模式
    // BthSevice.write(valuesArr);
    // var data = [0x7e, 0x07, 0x05, 0x03, 0xe6, 0x00, 0x4d, 0x00, 0xef]; // 设置色盘
    // BthSevice.write(data);

    $scope.indicatorOption = {
        radius : 80,
        percentage :true,
        barColor : "#009CFF",
        barBgColor: "#CCEBFF",
        roundCorner: true,
        barWidth: 7,
        displayNumber: false,
        // frameTime: 1500,
        initValue : 0
    };

    $scope.ts = $rootScope.Yzb_time; //80秒
    var every = 100/$scope.ts;
    var progress = 0; // 进度
    function down() {
        if($scope.ts == 0){
            $interval.cancel(Count);
            $scope.resetTime(); // 重置
            BthSevice.closeLED(); // 完成后关闭led
            $rootScope.start = false; //停止运行
            maintain_sta = 1;
            $http.post($myconfig.api.base_host + '/users/maintain' ,{
                "user_id":$scope.userMes.id,
                "dev_name":deviceName,
                "mode":mode,
                "duration":$scope.HYBsec,
                "state":maintain_sta,
                "createtime":ThisTime
            }).then(function(res){
                // console.log(res.data.data);
                $scope.HYBsec = 0;
                $http.get($myconfig.api.base_host + '/physicallogs/finsh?user_id=' + $scope.userMes.id + '&mode=' + mode + '&dev_name=' + deviceName)
                .then(function(res){
                    // console.log(res.data.data);
                    res.data.data.duration = ((res.data.data.duration) / 60).toFixed(0);
                    res.data.data.totle_duration = ((res.data.data.totle_duration) / 60).toFixed(0);
                    $rootScope.finsh = res.data.data;
                });
            });
            $state.go('app.UpKeepingEnd', {
                type: $scope.title
            });
            return;
        }
        $scope.HYBsec++;
        progress += every; //每次执行进度条动多少
        radialIndicatorInstance['indicator1'].animate(progress);
        $scope.ts = $scope.ts - 1;
    }

    $scope.resetTime = function () {
        $scope.ts = typeTime;
        progress = 0;
        radialIndicatorInstance['indicator1'].animate(0); //重置为原点
        $("#zhuan1").removeClass('tf');
        if (!$scope.$$phase && !$scope.$root.$$phase) {
            $scope.$apply();
        }
    }

    var Count;
    var myPopup;
    $("#zhuan1").css("animation-play-state", "paused");
    $scope.cancelTimer = function() {
        $scope.msg = '您确定要结束本次保养吗？';
        myPopup = $ionicPopup.show({
            templateUrl: 'templates/bitongbao/module/tishi.html',
            scope: $scope
        });
        $scope.myclose = function() {
            myPopup.close();
        };
        $scope.ok = function() {
            myPopup.close();
            $rootScope.start = false;
            $interval.cancel(Count);
            $scope.resetTime();
            $scope.state = '开始';
            BthSevice.closeLED(); // 完成后关闭led
            $scope.changePage = true;
            maintain_sta = '0';
            $http.post($myconfig.api.base_host + '/users/maintain' ,{
                "user_id":$scope.userMes.id,
                "dev_name":deviceName,
                "mode":mode,
                "duration":$scope.HYBsec,
                "state":maintain_sta,
                "createtime":ThisTime
            }).then(function(res){
                // console.log(res.data.data);
                $scope.HYBsec = 0;
            });
            $state.go('app.YanZhiBao');
        };

    };

    $scope.state = '开始';
    $scope.started = function() {
        if($scope.state == '开始'){
            $rootScope.start = true; //启动状态
            $scope.state = '暂停';
            BthSevice.write(valuesArr); // 开始之后亮起设置led
            $interval.cancel(Count);
            $scope.resetTime();
            $("#zhuan1").addClass('tf');
            Count = $interval(down, 1000);
            $("#zhuan1").css("animation-play-state", "running");
        }else if($scope.state == '暂停'){
            BthSevice.closeLED(); // 完成后关闭led
            $rootScope.start = false; //暂停状态
            $scope.state = '继续';
            $interval.cancel(Count);
            $("#zhuan1").addClass('tf');
            // Count = $interval(down, 1000);
            $("#zhuan1").css("animation-play-state", "paused");
        }else{
            $rootScope.start = true; //暂停状态
            $scope.state = '暂停';
            Count = $interval(down, 1000);
            $("#zhuan1").css("animation-play-state", "running");
        }
    };

    $scope.$on('$destroy', function() {
        $interval.cancel(Count);
    });

    $scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, options) {
        // console.log(toState.name + '~~~~~~~~~~~~~~~~~~');
        if (toState.name == 'app.YanZhiBao' && !$scope.changePage && $rootScope.start == true) {
            event.preventDefault();
            $scope.msg = '您确定要结束本次保养吗？';
            myPopup = $ionicPopup.show({
                templateUrl: 'templates/bitongbao/module/tishi.html',
                scope: $scope
            });
            $scope.myclose = function() {
                myPopup.close();
            };
            $scope.ok = function() {
                myPopup.close();
                $rootScope.start = false;
                maintain_sta = '0';
                $http.post($myconfig.api.base_host + '/users/maintain' ,{
                    "user_id":$scope.userMes.id,
                    "dev_name":deviceName,
                    "mode":mode,
                    "duration":$scope.HYBsec,
                    "state":maintain_sta,
                    "createtime":ThisTime
                }).then(function(res){
                    // console.log(res.data.data);
                    $scope.HYBsec = 0;
                });
                $interval.cancel(Count);
                $scope.resetTime();
                $("#zhuan1").css("animation-play-state", "paused");
                BthSevice.closeLED(); // 完成后关闭led
                $scope.changePage = true;
                $state.go('app.YanZhiBao');
            };
        }else if(myPopup){
            myPopup.close();
        }
    });

    $scope.goBack = function () {
        $state.go('app.YanZhiBao');
    };

    $scope.$on('$ionicView.enter', function(){

        $ionicConfig.views.swipeBackEnabled(false);

    });
});
